package com.interceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.service.infocenter.RulePointService;

public class PointInterceptor extends HandlerInterceptorAdapter {

	@Resource(name = "rulePointServiceImp")
	private RulePointService rulePointService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String rolename = (String) request.getSession().getAttribute("role_name");
		
		if(rolename !=null &&!rolename.equals("")){
			if(!rolename.equals("知识管理员")&&!rolename.equals("专家")&&!rolename.equals("超级知识管理员"))
			{
				String  url= request.getRequestURL().toString();		
				String[] urls = url.split("\\/");
				String name = urls[urls.length - 1];
				if("p_downloadn".equals(name)){
					String userid = (String) request.getSession().getAttribute("user_id");
					String infoid = request.getParameter("infoid");
					int i = rulePointService.infodownload(userid, infoid);
					if (i == 0) {
						return false;
					} else
						return true;
				}
			}
		}
		return true;

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		String rolename = (String) request.getSession().getAttribute("role_name");
		String  url= request.getRequestURL().toString();		
		String[] urls = url.split("\\/");
		String name = urls[urls.length - 1];
		String userid = (String) request.getSession().getAttribute("user_id");
		System.out.println(rolename);
		System.out.println("==============角色名称: 3、afterCompletion================"+rolename);
		if(rolename !=null &&!rolename.equals("")){
			if(!rolename.equals("知识管理员")&&!rolename.equals("专家")&&!rolename.equals("超级知识管理员")){
				if("getUserLogin".equals(name)){
					rulePointService.login(userid);
				}
				else if ("singleuploadInfo".equals(name)) {
					rulePointService.infoupload(userid);
				}
				else if ("illegalMessage".equals(name)) {
					String touserid = request.getParameter("userid");
					rulePointService.illegalMessage(touserid);
				}
				else if ("waterMessage".equals(name)) {
					String touseridd = request.getParameter("userid");
					rulePointService.waterMessage(touseridd);
				}
				else if ("addMessage".equals(name)) {
					String content = request.getParameter("message_content");
					rulePointService.addMessage(userid,content);
				}
				/*else if("p_downloadn".equals(name)){
					String infoid = request.getParameter("infoid");
				    rulePointService.infodownload(userid, infoid);
					
				}*/
			}
		}
	
	}
}
